package name.as3.simpleUIComponents.controls
{
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

import name.as3.simpleUIComponents.core.SimpleSprite;
import name.as3.simpleUIComponents.defaultSkins.DefaultSimpleButtonSkin;
import name.as3.simpleUIComponents.interfaces.skinParts.IButtonStateDisplayObject;
import name.as3.simpleUIComponents.interfaces.skins.ISimpleButtonSkin;
import name.as3.simpleUIComponents.types.Size;

public class SimpleUIButton extends SimpleSprite
{
	private var m_size:Size = new Size(80, 20);
	private var m_background:DisplayObject;
	private var m_partState:IButtonStateDisplayObject;
	private var m_strText:String;
	private var m_ttf:TextField;
	/**
	 *
	 */
	public function SimpleUIButton()
	{
		super();
		m_ttf = new TextField();
		m_ttf.mouseEnabled = false;
		m_ttf.autoSize = TextFieldAutoSize.LEFT;
		this.skin = new DefaultSimpleButtonSkin();
		this.setSize(m_size.width, m_size.height);
		super.addEventListener(MouseEvent.ROLL_OVER, rollOver_this);
		super.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown_this);
	}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// event
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	private function rollOver_this(e:MouseEvent):void
	{
		super.addEventListener(MouseEvent.ROLL_OUT, rollOut_this);
		if (m_partState)
		{
			m_partState.over();
		}
	}
	/**
	 *
	 */
	private function mouseDown_this(e:MouseEvent):void
	{
		super.addEventListener(MouseEvent.MOUSE_UP, rollOut_this);
		if (m_partState)
		{
			m_partState.down();
		}
	}
	/**
	 *
	 */
	private function rollOut_this(e:MouseEvent):void
	{
		super.removeEventListener(MouseEvent.ROLL_OUT, rollOut_this);
		if (m_partState)
		{
			m_partState.up();
		}
	}
//==================================================================================================
// public
//==================================================================================================
	public function set skin(value:ISimpleButtonSkin):void
	{
		if (value)
		{
			m_partState = value.backgroundImage;
			m_background = value.backgroundImage as DisplayObject;
			super.addChild(m_background);
			super.addChild(m_ttf);
		}
	}
	public function set text(value:String):void
	{
		m_strText = value;
		m_ttf.text = value;
		if (m_background)
		{
			this.setSize(m_background.width, m_background.height);
		}
	}
	public function get text():String
	{
		return m_strText;
	}
	
	override public function setSize(nW:Number, nH:Number):void
	{
		if (m_background)
		{
			m_background.width = nW;
			m_background.height = nH;
			m_ttf.x = (m_background.width - m_ttf.width) * .5;
			m_ttf.y = (m_background.height - m_ttf.height) * .5;
		}
	}
}
}